package com.jianzh5.page;

/**
 * 分页计算辅助类
 */
public class PageUtil {

    public static PageView createPage(int size,int total,int cPage) {
        size = getSize(size);
        cPage = getCPage(cPage);
        int tPage = getTPage(size, total);
        int beginIndex = getBIndex(size, cPage);
        boolean hasPrePage = getHasPrePage(cPage);
        boolean hasNextPage = getHasNextPage(tPage, cPage);
        return new PageView(size, total, tPage, cPage, beginIndex, hasPrePage, hasNextPage);
    }

    public static PageView createPage(PageView page , int total){
        int size = getSize(page.getSize());
        int cPage = getCPage(page.getCpage());
        int tPage = getTPage(size, total);
        int bIndex = getBIndex(size, cPage);
        boolean hasPrePage = getHasPrePage(cPage);
        boolean hasNextPage = getHasNextPage(tPage, cPage);
        return new PageView(size, total, tPage, cPage, bIndex, hasPrePage, hasNextPage);
    }

    //每页显示记录数
    public static int getSize(int size){
        return size == 0 ? 10 : size;
    }

    //当前页数
    public static int getCPage(int cPage){
        return cPage == 0 ? 1 : cPage;
    }

    //总页数
    public static int getTPage(int size,int total){
        int totalPage = 0;
        if(total % size == 0) {
            totalPage = total / size;
        } else {
            totalPage = total / size + 1;
        }
        return totalPage;
    }

    //起始位置
    public static int getBIndex(int size ,int cPage){
        return (cPage - 1) * size;
    }

    //上一页
    public static boolean getHasPrePage(int cPage){
        return cPage == 1 ? false : true;
    }

    //下一页
    public static boolean getHasNextPage(int tPage , int cPage){
        return cPage == tPage || tPage == 0 ? false : true;
    }

}
